<link rel="stylesheet" href="/static/css/sweetalert.css">
    <div class="confirm_content margin_top_foot">
        <header class="header fixed_top bg_mx_green"></header>
        <form class="order_info"></form>
        <div class="bg_white absolute all carrier_list hide" style="z-index:10"></div>
        <div class="bg_white absolute all deliver_list hide" style="z-index:10"></div>
    </div>
    <div class="toast"></div>
    <!-- toast模版 -->
    <script type="text/template" charset="utf-8" id='toast_template'>
        <i class="iconfont icon-toast{{=it.icon}}"></i>
        <div class="toast_text">{{=it.text}}</div>
    </script>
    <script type="text/template" id='goods_template'>
        <div class="font_16">
            {{for(var i in it.cart){}}
                {{if(it.cart[i].selected){}}
                <div class="cart_item flex bg_white padding_10_15 border_b">
                    <div class="width_100">
                        <img class="circle_5" src="{{=it.cart[i].img}}" alt="">
                    </div>
                    <div class="left_100 padding_l_10">
                        <div class="margin_b_10">
                            <span>{{=it.cart[i].title}}</span>
                            <input type="hidden" name="goodsid[]" value="{{=it.cart[i].id}}">
                        </div>
                        <div class="font_10 warning font_18">￥{{=it.cart[i].price}}</div>
                        <div class="flex_justify light">
                            {{if(it.cart[i].o_price){}}
                                <div class="linethrough">{{=it.cart[i].o_price}}</div>
                            {{}}}
                            <div class="">x{{=it.cart[i].num}}</div>
                            <input type="hidden" name="goodsnum[]" value="{{=it.cart[i].num}}">
                        </div>
                    </div>
                </div>
                {{}}}
            {{} }}
            <div class="padding_10_15 bg_white border_b flex_justify">
                <span>配送方式</span>
                <div class="font_12 h_justify">
                    <div class="label select_sendtype {{if(it.sendtype=='1'){}}label_danger{{}else{}}label_grey{{}}} margin_r_5" data-n="1">快递配送</div>
                    <div class="label select_sendtype {{if(it.sendtype=='2'){}}label_danger{{}else{}}label_grey{{}}}" data-n="2">上门自提</div>
                </div>
            </div>
            {{if(it.sendtype=='1'){}}
                <div class="type_delivery">
                    {{if(it.address){}}
                    <div class="padding_10_15 arrow_right bg_white border_b" onclick="select_address()">
                        <div class="font_16">收货人：<span class="">{{=it.address.realname}}({{=it.address.mobile}})</span></div>
                        <div class="text_info">地址：{{=it.address.province}}{{=it.address.city}}{{=it.address.area}}{{=it.address.address}}</div>
                        <input type="hidden" name="address_id" value="{{=it.address.id}}">
                    </div>
                    {{}else{}}
                        <div class="padding_10_15 arrow_right bg_white border_b" onclick="select_address()">
                            <div class="font_16">收货人：<span class="text_info">没有默认地址，快去添加吧</span></div>
                            <input type="hidden" name="address_id" value="" request title="地址">
                        </div>
                    {{}}}
                    <div class="padding_10_15 arrow_right bg_white border_b flex_justify open_layer" data-i="2">
                        <span>快递类型</span>
                        <span class="light padding_r_10">{{=it.delivery}}</span>
                        <input type="hidden" name="dispach" value="{{=it.delivery}}">
                    </div>
                </div>
            {{}}}
            {{if(it.sendtype=='2'){}}
                <div class="type_self">
                    <div class="padding_10_15 border_b arrow_right bg_white open_layer" data-i="1">

                        <div class="text_info">地址：{{=it.carrier.address}}</div>
                        <div class="text_info">备注：{{=it.carrier.remark}}</div>
                        <input type="hidden" name="carrier_id" value="{{=it.carrier.id}}">
                    </div>
                    <div class="padding_10_15 border_b flex bg_white">
                        <div class="width_100">提货人姓名：</div>
                        <div class="left_100">
                            <input type="text" placeholder="请输入您的姓名" value="<{$ziti['realname']}>" name="carrier_name" request title="姓名">
                        </div>
                    </div>
                    <div class="padding_10_15 border_b bg_white flex">
                        <div class="width_100">提货人手机：</div>
                        <div class="left_100">
                            <input type="text" placeholder="请输入您的手机号" value="<{$ziti['mobile']}>" name="carrier_tel" request title="手机号" data-pattern="^(0|86|17951)?(13\d|15[0-35-9]|17[678]|18\d|14[57])\d{8}$" data-tips="11位手机号">
                        </div>
                    </div>
                </div>
            {{}}}
            <div class="padding_10_15 border_b bg_white flex">
                <div class="width_50">备注：</div>
                <div class="left_50">
                    <textarea placeholder="请输入您的备注" rows="3" name="remark"></textarea>
                </div>
            </div>
            {{if(it.credit){}}
            <div class="padding_10_15 border_b bg_white h_justify font_14">
                <label class=""><input type="checkbox" name="usecredit" class="checkbox" value="{{=it.credit}}"> 是否使用积分抵扣</label>
                <div class="light">{{=it.credit}}积分可抵{{=it.reduce}}元</div>
            </div>
            {{}}}
            <div class="padding_10_15 bg_white border_b h_justify">
                <span>运费</span>
                <span class="warning">￥{{=it.freight.toFixed(2)}}</span>
            </div>
            <div class="padding_10_15 bg_white border_b text_right">商品合计：<span class="warning">￥{{=it.totalprice.toFixed(2)}}</span></div>
        </div>
        <footer class="confirm_foot fixed_bottom bg_white">
            <div class="border_t_mx padding_h_15 flex_justify confirm_btn">
                <div class="font_20 warning allprice">￥{{=it.allprice.toFixed(2)}}</div>
                <input type="hidden" name="sendtype" value="{{=it.sendtype}}">
                <button class="btn btn_danger gobuy">确认支付</button>
            </div>
        </footer>
        <input name="_csrf" type="hidden" id="_csrf" value="<{$app->request->csrfToken}>">
    </script>
    <script type="text/template" id='carrier_template'>
        <ul class="list">
            {{ for(var i in it){ }}
            <li class="border_b padding_10_15">
                <label class="v_center select_para" data-i="{{=i}}" data-n="1">
                    <input type="radio" name="carrierid" class="radio width_30" value="{{=i}}" {{if(it[i].selected){}}checked{{ } }}>
                    <div class="left_30 padding_l_10">
                        <div class="">地址：{{=it[i].address}}</div>
                        <div class="text_info">备注：{{=it[i].remark}}</div>
                    </div>
                </label>
            </li>
            {{ } }}
        </ul>
    </script>
    <script type="text/template" id='delivery_template'>
        <ul class="list">
            {{ for(var i in it){ }}
            <li class="border_b padding_10_15">
                <label class="light select_para" data-i="{{=i}}" data-n="2">
                    <input type="radio" name="deliveryid" class="radio" value="{{=i}}" {{if(it[i].selected){}}checked{{ } }}>
                    <span class="padding_l_10">{{=i}}快递</span>
                </label>
            </li>
            {{ } }}
        </ul>
    </script>
    <script src="/static/js/sweetalert-dev.js"></script>
    <script>
            //是否可以提交数据的判断标志
            var iscansubmit = true;
        // 获取数据
            var delivery_list = <{$delivery_list}>;console.log(delivery_list);
            if(delivery_list.length <=0){
                alert("暂时没有配送方式，请等待");
                location.href = "/shop/site/index";
            }
            var carrier_list = <{$since}>;
            var order_info = {sendtype:1,address:<{$address}>,delivery:Object.keys(delivery_list)[0],carrier:carrier_list[0],allprice:0,freight:0,credit:<{$score}>,reduce:<{$reduce}>};
            order_info.cart = {};
            console.log(order_info);
        // 页面初始化//是否直接购买//如果url有地址参数(order_info)传入该地址
            if(GetQueryString("type") && GetQueryString("type") == "gobuy"){
                var goods = get_data("buy");
                order_info.cart["1"] = goods;
                order_info.totalprice = F(goods.price)*goods.num;
                order_info.totalweight = F(goods.weight)*goods.num;
            }else{
                var carts = get_data("cart");
                console.log(carts);
                delete carts.total;
                delete carts.allselected;
                delete carts.number;
                for(var i in carts){
                    if(i!=='totalprice' && i!=='totalweight'){
                        order_info.cart[i] = carts[i]
                    }else{
                        order_info[i] = carts[i]
                    }
                    order_info.totalprice = carts.totalprice
                    order_info.totalweight = carts.totalweight;

                }
            }
            cal_total(order_info);
//            console.log(order_info)
            var evalgoods = doT.template($("#goods_template").text());
            var evalcarrier = doT.template($("#carrier_template").html());
            var evaldelivery = doT.template($("#delivery_template").text());
            var evalToast = doT.template($("#toast_template").text());
            loaddata(".order_info",evalgoods,order_info);
            loaddata(".carrier_list",evalcarrier,carrier_list);
            loaddata(".deliver_list",evaldelivery,delivery_list);
            loadheadbar("订单确认");
            // localStorage.removeItem("cart");// 删除本地存储
        // 选择快递或自提
            $(".order_info").on("click",".select_sendtype",function(){
                var n = $(this).data("n");
                order_info.sendtype = n;
                loaddata(".order_info",evalgoods,cal_total(order_info));
            })
        // 计算总价
            function cal_total(d){
                if(d.sendtype==1){
                    var delivery_one = delivery_list[d.delivery];
                    d.freight = I(d.totalweight)<I(delivery_one.weight)?F(delivery_one.price):F(delivery_one.price)+Math.ceil((I(d.totalweight)-I(delivery_one.weight))/I(delivery_one.secondweight))*F(delivery_one.secondprice);
                }else{
                    d.freight = 0;
                }
                d.allprice = d.totalprice + d.freight;
                return d;
            }
        // 打开选择浮层
            $(".confirm_content").on("click",".open_layer",function(){
                var n = $(this).data("i");
                $("body").scrollTop(0).css("overflow","hidden");
                switch(n){
                    case(1):var ele = ".carrier_list";break;
                    case(2):var ele = ".deliver_list";break;
                }
                $(ele).show();
                $(window).on('touchmove', function (e) {
                    e.preventDefault();
                });
            })
        // 选择自提点、快递方式
            $(".confirm_content").on("click",".select_para",function(){
                var n = $(this).data("n"),i = $(this).data("i");
                switch(n){
                    case(1):var ele = ".carrier_list";order_info.carrier = carrier_list[i];break;
                    case(2):var ele = ".deliver_list";order_info.delivery = i;break;
                }
                $(ele).hide();
                $("body").css("overflow","auto");
                $(window).unbind('touchmove');
                loaddata(".order_info",evalgoods,cal_total(order_info));
            })
        // 选择地址
            function select_address(){
                    if(GetQueryString("type") && GetQueryString("type") == "gobuy") {
                        window.location.href = "/business/user/address/index?type=select&gobuy=true";
                    }else{
                        window.location.href = "/business/user/address/index?type=select";
                    }
            }
        // 是否使用积分抵扣
        $(document).on("change","input[name='usecredit']",function(){//修改成这样的写法
            if(order_info.allprice < order_info.reduce){
                iscansubmit = false;
                //抵消的金额大于商品的价格
                toast("attention","商品金额小于您抵消的金额",function(){
                    $("input[name='usecredit']").removeAttr('checked');
                    iscansubmit = true;
                });
                return false;
            }
            if($("input[name='usecredit']:checked").length){
                order_info.allprice -= order_info.reduce;
            }else{
                order_info.allprice += order_info.reduce;
            }
            $(".allprice").html("￥"+order_info.allprice.toFixed(2))
        });



        //此方法切换失效
        /*$("input[name='usecredit']").change(function(){
            if($("input[name='usecredit']:checked").length){
                order_info.allprice -= order_info.reduce;
            }else{
                order_info.allprice += order_info.reduce;
            }
            $(".allprice").html("￥"+order_info.allprice.toFixed(2))
        })*/


        // 提交订单
            var submited = false;
            $(".order_info").submit(function(e){

                console.log(iscansubmit);
                //不能提交
                if(!iscansubmit){
                    return false;
                }

                e.preventDefault();
                with(this){
                    for (var i = 0; i < this.length; i++) {
                        if($(this[i]).attr("request")==""){
                            if(!checkone(this[i])){
                                return false;
                            }
                        }
                    }
                    if(submited) return false;

                    var data_submit = $(".order_info").serializeObject();console.log(data_submit)
                    $.post("/shop/order/pre-pay",data_submit,function(res){
                        submited = true;
                        if(res.status == 0 && res.url){
                            swal({
                                title: res.msg,
                                type: "success",
                                timer: 2000,
                                showConfirmButton: false
                            },
                            function() {
                                set_data({},'cart');
                                location.href = res.url;
                            });
                        }else{
                            submited = false;
                            swal({
                                    title: "温馨提示",
                                    text:res.msg,
                                    type: "warning",
                                    timer: 2000,
                                    showConfirmButton: false
                                });
                        }

                        // toast("attention",res.msg,function(){
                        //     if(res.status == 0 && res.url){
                        //         set_data({},'cart');
                        //         location.href = res.url;
                        //
                        //     }
                        // });
                    },"json")
                }
            })
    </script>


